package com.example.wunai;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;

import androidx.appcompat.app.AppCompatActivity;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private ImageButton[] imageButtons = new ImageButton[9];
    private ImageButton clickedButton = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化九个 ImageButton
        imageButtons[0] = findViewById(R.id.b1);
        imageButtons[1] = findViewById(R.id.b2);
        imageButtons[2] = findViewById(R.id.b3);
        imageButtons[3] = findViewById(R.id.b4);
        imageButtons[4] = findViewById(R.id.b5);
        imageButtons[5] = findViewById(R.id.b6);
        imageButtons[6] = findViewById(R.id.b7);
        imageButtons[7] = findViewById(R.id.b8);
        imageButtons[8] = findViewById(R.id.b9);

        imageButtons[0].setTag(1);
        imageButtons[1].setTag(2);
        imageButtons[2].setTag(3);
        imageButtons[3].setTag(4);
        imageButtons[4].setTag(5);
        imageButtons[5].setTag(6);
        imageButtons[6].setTag(7);
        imageButtons[7].setTag(8);
        imageButtons[8].setTag(9);
        for(int i=0;i<4;i++)
        {
            Random random=new Random();
            int arraySize=imageButtons.length;
            int j=random.nextInt(arraySize);
            int k=random.nextInt(arraySize);
            if(j==k)
            {
                k=random.nextInt(arraySize);
            }
            ImageButton button1=imageButtons[j];
            ImageButton button2=imageButtons[k];

            Drawable drawable1 = button1.getDrawable();
            Drawable drawable2 = button2.getDrawable();

            int tag1Before = button1.getTag()!= null? (int) button1.getTag() : -1;
            int tag2Before = button2.getTag()!= null? (int) button2.getTag() : -1;
            button1.setTag(tag2Before);
            button2.setTag(tag1Before);

            button1.setImageDrawable(drawable2);
            button2.setImageDrawable(drawable1);
        }
        for (int i = 0; i < 9; i++) {

            int finalI = i;
            imageButtons[i].setOnClickListener(v -> {
                Log.d("ButtonClick", "Button clicked: " + finalI);
                handleImageButtonClick(imageButtons[finalI]);
                if(check())
                {
                    Toast.makeText(MainActivity.this, "成功通关", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                    startActivity(intent);
                }
            });
            }
        }
private boolean check(){
    ImageButton button1 =findViewById(R.id.b1);
        ImageButton button2 =findViewById(R.id.b2);
        ImageButton button3 =findViewById(R.id.b3);
        ImageButton button4 =findViewById(R.id.b4);
        ImageButton button5 =findViewById(R.id.b5);
        ImageButton button6 =findViewById(R.id.b6);
        ImageButton button7 =findViewById(R.id.b7);
        ImageButton button8 =findViewById(R.id.b8);
        ImageButton button9 =findViewById(R.id.b9);
        System.out.println(
                "1:"+(int)button1.getTag()+
                        "2:"+(int)button2.getTag()+
                        "3:"+(int)button3.getTag()+
                        "4:"+(int)button4.getTag()+
                        "5:"+(int)button5.getTag()+
                        "6:"+(int)button6.getTag()+
                        "7:"+(int)button7.getTag()+
                        "8:"+(int)button8.getTag()+
                        "9:"+(int)button9.getTag()

        );
        if(     (int)button1.getTag()==1
                &&(int)button2.getTag()==2
                &&(int)button3.getTag()==3
                &&(int)button4.getTag()==4
                &&(int)button5.getTag()==5
                &&(int)button6.getTag()==6
                &&(int)button7.getTag()==7
                &&(int)button8.getTag()==8
                &&(int)button9.getTag()==9
        )
            return true;
        else return false;
}

//    private boolean check() {
//        ImageButton button1 =findViewById(R.id.b1);
//        ImageButton button2 =findViewById(R.id.b2);
//        ImageButton button3 =findViewById(R.id.b3);
//        ImageButton button4 =findViewById(R.id.b4);
//        ImageButton button5 =findViewById(R.id.b5);
//        ImageButton button6 =findViewById(R.id.b6);
//        ImageButton button7 =findViewById(R.id.b7);
//        ImageButton button8 =findViewById(R.id.b8);
//        ImageButton button9 =findViewById(R.id.b9);
//        Resources resources = getResources();
//        float density = resources.getDisplayMetrics().density;
//        int[] bt1=new int[2];
//        int[] bt2=new int[2];
//        int[] bt3=new int[2];
//        int[] bt4=new int[2];
//        int[] bt5=new int[2];
//        int[] bt6=new int[2];
//        int[] bt7=new int[2];
//        int[] bt8=new int[2];
//        int[] bt9=new int[2];
//        button1.getLocationOnScreen(bt1);
//        button2.getLocationOnScreen(bt2);
//        button3.getLocationOnScreen(bt3);
//        button4.getLocationOnScreen(bt4);
//        button5.getLocationOnScreen(bt5);
//        button6.getLocationOnScreen(bt6);
//        button7.getLocationOnScreen(bt7);
//        button8.getLocationOnScreen(bt8);
//        button9.getLocationOnScreen(bt9);
//        float x=108*density;
//        float y=104*density;
//        if(Math.abs(bt1[0]+x-bt5[0]) < 5 && Math.abs(bt1[1]+y-bt5[1]) <5
//                && Math.abs(bt5[0]-bt2[0]) <5 && Math.abs(bt2[1]+y-bt5[1]) <5
//                && Math.abs(bt5[0]+x-bt3[0]) <5 && Math.abs(bt5[1]-(bt3[1]+y)) <5
//                && Math.abs(bt5[0]-x-bt4[0]) <5 && Math.abs(bt5[1]-bt4[1]) <5
//                && Math.abs(bt5[0]+x-bt6[0]) <5 && Math.abs(bt5[1]-bt6[1]) <5
//                && Math.abs(bt5[0]- bt8[0]) <5 && Math.abs(bt5[1]+y-bt8[1]) <5
//                && Math.abs(bt5[0]+x-bt9[0]) <5 && Math.abs(bt5[1]+y-bt9[1]) <5)
//            return true;
//        else  return false;
//    }

    private void handleImageButtonClick(ImageButton button) {

        // 找到另一个被点击的按钮位置（这里假设已经有一个按钮被点击过）
        ImageButton otherButton =findOtherClickedButton();
        if (otherButton == null) {
            // 如果没有另一个被点击的按钮，存储当前点击的按钮位置
            storeClickedButton(button);
            System.out.println("存储：-----------------"+button.getId());
        }
        else {
//            int image1 = button.getDrawable().getConstantState().newDrawable().getCurrent().hashCode();
//            int image2 = otherButton.getDrawable().getConstantState().newDrawable().getCurrent().hashCode();
//            System.out.println("交换：-----------------"+button.getId()+",image:"+image1);
//            System.out.println("交换：-----------------"+otherButton.getId()+",image:"+image1);
//
//            Log.d("ImageSwap", "Before swap, button has image1: " + image1);
//            Log.d("ImageSwap", "Other button has image2: " + image2);
//            button.setImageResource(image2);
//            otherButton.setImageResource(image1);



            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Drawable drawable1 = button.getDrawable();
                    Drawable drawable2 = otherButton.getDrawable();
                    int tag1Before = button.getTag()!= null? (int) button.getTag() : -1;
                    int tag2Before = otherButton.getTag()!= null? (int)otherButton.getTag() : -1;
                    button.setTag(tag2Before);
                    otherButton.setTag(tag1Before);

                    button.setImageDrawable(drawable2);
                    otherButton.setImageDrawable(drawable1);

                    Log.d("ImageSwap", "After swap, button has new image: " + button.getDrawable().getConstantState().newDrawable().getCurrent().hashCode());
                    Log.d("ImageSwap", "Other button has new image: " + otherButton.getDrawable().getConstantState().newDrawable().getCurrent().hashCode());
                }
            }, 500);
            // 清除已存储的点击位置
            clearClickedButton();
        }

        }


    // 存储点击的按钮

    private void storeClickedButton(ImageButton button) {
        clickedButton = button;
    }

    private ImageButton findOtherClickedButton() {
        return clickedButton;
    }

    private void clearClickedButton() {
        clickedButton = null;
    }
}